//
//  RealmViewController.m
//  LockTest
//
//  Created by ZhangBo on 2020/10/10.
//  Copyright © 2020 ZhangBo. All rights reserved.
//

#import "RealmViewController.h"
#import <Realm/Realm.h>

@interface Dog : RLMObject
@property NSString *name;
@property NSInteger age;
@end

@implementation Dog
@end

RLM_ARRAY_TYPE(Dog);

@interface Person1 : RLMObject
@property NSString *name;
@property RLMArray<Dog *> <Dog> *dogs;
@end

@implementation Person1
@end

@interface RealmViewController ()

@end

@implementation RealmViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    Dog *dog1 = [[Dog alloc] init];
    dog1.name = @"peter";
    dog1.age = 1;

    Person1 *person = [[Person1 alloc] init];
    person.name = @"wingsmm";
    [person.dogs addObject:dog1];


    Dog *dog2 = [[Dog alloc] init];
    dog2.name = @"Hans";
    dog2.age = 3;

    NSLog(@"%@", person);

    RLMResults *results = [Dog objectsWhere:@"age <= 3"];
    NSLog(@"%@", results);

    RLMRealm *r = [RLMRealm defaultRealm];

    [r transactionWithBlock:^{
        [r deleteAllObjects];
    }];

    [r transactionWithBlock:^{
        [r addObject:dog1];
        [r addObject:dog2];
    }];

    NSLog(@"%@", results);

}

@end
